//
//  EScrollerView.m
//  icoiniPad
//
//  Created by Ethan on 12-11-24.
//
//

#import "EScrollerView.h"
@implementation EScrollerView
@synthesize delegate;

- (void)dealloc
{
	[scrollView release];
    [noteTitle release];
	delegate=nil;
    if (pageControl) {
        [pageControl release];
    }
    if (imageArray) {
        [imageArray release];
        imageArray=nil;
    }
    if (titleArray) {
        [titleArray release];
        titleArray=nil;
    }
    [super dealloc];
}

- (id)initWithFrameRect:(CGRect)rect ImageArray:(NSArray *)imgArr TitleArray:(NSArray *)titArr
{
    
	if ((self=[super initWithFrame:rect])) {
        self.userInteractionEnabled=YES;
        titleArray=[titArr retain];
        NSMutableArray *tempArray=[NSMutableArray arrayWithArray:imgArr];
        [tempArray insertObject:[imgArr objectAtIndex:([imgArr count]-1)] atIndex:0];
        [tempArray addObject:[imgArr objectAtIndex:0]];
		imageArray=[[NSArray arrayWithArray:tempArray] retain];
		viewSize = rect;
        NSUInteger pageCount=[imageArray count];
        scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, viewSize.size.width, viewSize.size.height)];
        scrollView.pagingEnabled = YES;
        scrollView.contentSize = CGSizeMake(viewSize.size.width * pageCount, viewSize.size.height);
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.showsVerticalScrollIndicator = NO;
        scrollView.scrollsToTop = NO;
        scrollView.delegate = self;
        for (int i=0; i<pageCount; i++) {
            NSString *imgURL=[imageArray objectAtIndex:i];
            NSString * utImgURL = [imgURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//            NSLog(@"imu = %@",imgURL);
            UIImageView *imgView=[[[UIImageView alloc] init]autorelease];
            if ([imgURL hasPrefix:@"https://"]||[imgURL hasPrefix:@"http://"])
            {
                //网络图片 请使用ego异步图片库
                NSLog(@"%@",imgURL);
                NSURL * url = [NSURL URLWithString:utImgURL];
                NSLog(@"%@",url);
                [imgView  sd_setImageWithURL:[NSURL URLWithString:utImgURL] placeholderImage:[UIImage imageNamed:@"top.jpg"]];
                
            }
            else
            {
                
                UIImage *img = [UIImage imageNamed:@"top.jpg"];
//                imgView.backgroundColor = [UIColor colorWithRed:0 green:0.66 blue:0.91 alpha:1];
                [imgView setImage:img];
            }
            
            [imgView setFrame:CGRectMake(viewSize.size.width*i, 0,viewSize.size.width, viewSize.size.height)];
            imgView.tag=i;
            UITapGestureRecognizer *Tap =[[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imagePressed:)] autorelease];
            [Tap setNumberOfTapsRequired:1];
            [Tap setNumberOfTouchesRequired:1];
            imgView.userInteractionEnabled=YES;
            [imgView addGestureRecognizer:Tap];
            [scrollView addSubview:imgView];
        }
        [scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];
        [self addSubview:scrollView];
 
        //说明文字层
        UIView *noteView=[[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height-28,self.bounds.size.width,33)];
        //[noteView setBackgroundColor:[UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.5]];
        [noteView setBackgroundColor:[UIColor clearColor]];
        float pageControlWidth=(pageCount-2)*10.0f+40.f;
        float pagecontrolHeight=20.0f;
//        pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake((self.frame.size.width-pageControlWidth-100),10, pageControlWidth, pagecontrolHeight)];
        
        pageControl=[[UIPageControl alloc]initWithFrame:CGRectMake((self.frame.size.width-pageControlWidth)/2,10, pageControlWidth, pagecontrolHeight)];
        pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
        pageControl.currentPageIndicatorTintColor = [UIColor whiteColor];
        pageControl.currentPage=0;
        pageControl.numberOfPages=(pageCount-2);
        pageControl.tag = 1111;
        [noteView addSubview:pageControl];
        
//        noteTitle=[[UILabel alloc] initWithFrame:CGRectMake(5, 6, self.frame.size.width-pageControlWidth-15, 20)];
//        [noteTitle setText:[titleArray objectAtIndex:0]];
//        [noteTitle setBackgroundColor:[UIColor clearColor]];
//        [noteTitle setFont:[UIFont systemFontOfSize:13]];
//        [noteView addSubview:noteTitle];
//        
        [self addSubview:noteView];
        [noteView release];
        
        if (imgArr.count != 0)
        {
            NSString * imageUrl = [imgArr objectAtIndex:0];
            if ([imageUrl hasPrefix:@"http"])
            {
                _timer = [NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(pages:andTimer:) userInfo:nil repeats:YES];
            }
            
        }
	}
	return self;
}

-(void)pages:(id)pageC andTimer:(NSTimer *)timer
{
    CGPoint newOffset = CGPointMake(scrollView.contentOffset.x+CGRectGetWidth(scrollView.frame), scrollView.contentOffset.y);
    //[UIView beginAnimations:nil context:NULL];
    //[UIView setAnimationDuration:1.0f];
    [scrollView setContentOffset:newOffset animated:YES];
    //[UIView commitAnimations];
    if (currentPageIndex== 0)
    {
        [scrollView setContentOffset:CGPointMake(([imageArray count]-2)*viewSize.size.width, 0)];
    }
    if (currentPageIndex==([imageArray count]-2)) {
        
        [scrollView setContentOffset:CGPointMake(0, 0)];
        currentPageIndex = 1;
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)sender
{
    CGFloat pageWidth = scrollView.frame.size.width;
    int page = floor((scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
    currentPageIndex=page;
       
    pageControl.currentPage=(page-1);
    
    int titleIndex = page-1;
    if (titleIndex == [titleArray count]) {
        titleIndex = 0;
    }
    if (titleIndex < 0) {
        titleIndex = (int)[titleArray count]-1;
    }
    [noteTitle setText:[titleArray objectAtIndex:titleIndex]];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)_scrollView
{
    if (currentPageIndex == 0) {
        [_scrollView setContentOffset:CGPointMake(([imageArray count]-2)*viewSize.size.width, 0)];
    }
    
    if (currentPageIndex == ([imageArray count]-1)) {
       
        [_scrollView setContentOffset:CGPointMake(viewSize.size.width, 0)];
    }
}

- (void)imagePressed:(UITapGestureRecognizer *)sender
{

    if ([delegate respondsToSelector:@selector(EScrollerViewDidClicked:)]) {
        [delegate EScrollerViewDidClicked:sender.view.tag];
    }
}
- (void)stopTimer
{
    if (self.timer != nil)
    {
        [self.timer invalidate];
        self.timer = nil;
    }
    
}
@end
